package com.cs797.project;

import java.util.List;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class HotSpotMapView extends MapActivity {

    private static final String TAG = "HotSpotterMapView";

    LinearLayout linearLayout;
    MapView mv;
    MapController mc;
    double towerLatitude;
    double towerLongitude;
    double msLatitude;
    double msLongitude;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "in onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotspotmap);

        mv = (MapView) findViewById(R.id.mapview);
        mc = mv.getController();
        mv.setBuiltInZoomControls(true);

        Bundle params = this.getIntent().getExtras();

        boolean curPosOnly = params.getBoolean("current_position_only");

        towerLatitude = params.getDouble("towerLatitude");
        towerLongitude = params.getDouble("towerLongitude");
        GeoPoint towerLocation = new GeoPoint(DoubleToE6(towerLatitude), DoubleToE6(towerLongitude));
        
        if (curPosOnly) {
            
            msLatitude = params.getDouble("msLatitude");
            msLongitude = params.getDouble("msLongitude");

            Log.d(TAG, "mv got tower lat: " + towerLatitude);
            Log.d(TAG, "mv got tower lon: " + towerLongitude);
            Log.d(TAG, "mv got ms lat: " + msLatitude);
            Log.d(TAG, "mv got ms lon: " + msLongitude);

            GeoPoint msLocation = new GeoPoint(DoubleToE6(msLatitude), DoubleToE6(msLongitude));

            // Add a marker at the map location.
            List<Overlay> listOfOverlays = mv.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(new Marker(towerLocation, this.getBaseContext(), R.drawable.tower));
            listOfOverlays.add(new Marker(msLocation, this.getBaseContext(), R.drawable.mobile_phone));
        } else {
            int dataPoints = params.getInt("numDataPoints");
            
            List<Overlay> listOfOverlays = mv.getOverlays();
            listOfOverlays.clear();
            
            listOfOverlays.add(new Marker(towerLocation, this.getBaseContext(), R.drawable.tower));
            for (int i = 1; i <= dataPoints; ++i) {
                GeoPoint gpt = new GeoPoint(
                        DoubleToE6(params.getDouble("msLatitude" + String.valueOf(i))),
                        DoubleToE6(params.getDouble("msLongitude" + String.valueOf(i))));
                int signalStrength = params.getInt("msSignalStrength" + String.valueOf(i));
                listOfOverlays.add(new CircleOverlay(gpt, signalStrength, this.getBaseContext()));
            }
        }

        mc.animateTo(towerLocation);
        mc.setZoom(16);
        
        // Force view to redraw.
        mv.invalidate();
    }

    private int DoubleToE6(double value) {
        return (int) (value * 1e6);
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}
